<?php

namespace XiaoZhuBundle\Controller\Api;

use AdminsBundle\Entity\Product;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use XiaoZhuBundle\Helper\MyController;
/**
 * @Route("/Api")
 */
class ProductController extends MyController
{
    /**
     * @Route("/Products", name="ApiProducts")
     */
    public function ApiProducts(Request $request)
    {
        //获取分页信息
        if (!$p = $this->getParamRequired("pagenum",$request)) {$p = 1;}
        if (!$perPage = $this->getParamRequired("pagesize ",$request)) {$perPage = 10;}
        $param["page"] = $p;
        $param["per_page"] = $perPage;

        //获取筛选信息
        if ($category = $this->getParamRequired("category",$request)) {
            $param["andWhere"]["cid"] = $category;
        }
        if ($isNew = $this->getParamRequired("isNew",$request)) {
            $param["andWhere"]["isNew"] = $isNew;
        }
        if ($isHot = $this->getParamRequired("isHot",$request)) {
            $param["andWhere"]["isHot"] = $isHot;
        }
        if ($isSale = $this->getParamRequired("isSale",$request)) {
            $param["andWhere"]["isSale"] = $isSale;
        }
        if ($isFamous = $this->getParamRequired("isFamous",$request)) {
            $param["andWhere"]["isFamous"] = $isFamous;
        }

        //获取排序信息
        $param["orderBy"]["id"] = "DESC";
        if ($price = $this->getParamRequired("price",$request)) {
            $param["orderBy"]["price"] = $price == 1 ? 1 : 0;
        }
        if ($sell = $this->getParamRequired("sell",$request)) {
            $param["orderBy"]["sell"] = $sell == 1 ? 1 : 0;
        }
        if ($create = $this->getParamRequired("create",$request)) {
            $param["orderBy"]["create"] = $create == 1 ? 1 : 0;
        }
        if ($collect = $this->getParamRequired("praise",$request)) {//点赞
            $param["orderBy"]["collect"] = $collect == 1 ? 1 : 0;
        }

        $Model = $this->get("xiao_zhu.product_model");
        $data = $Model->listProduct($param);

        $re = array();
        foreach ($data as $d) {
            $re[] = array(
                "id"        => $d["id"],
                "img"       => $d["cover"],
                "title"     => $d["name"],
                "classes"     => $d["cid"],
                "price"     => $d["price"],
                "oprice"    => $d["marketPrice"],
                "praise"    => $d["collect"],
                "isNew"    => $d["isNew"],
                "isHot"    => $d["isHot"],
                "isSale"    => ceil($d["price"]/$d["marketPrice"]*10),
                "isFamous"    => $d["isFamous"],
            );
        }

        return $this->json_out(1,"",$re);
    }


    /**
     * @Route("/Product/Show", name="ApiProductShow")
     */
    public function ApiProductShow(Request $request)
    {
        if (!$id = $this->getParamRequired("id",$request)) {
            return $this->json_out(0,"缺少参数","ID");
        }
        $ProductModel = $this->get("xiao_zhu.product_model");
        $product = $ProductModel->showProduct($id);
        //分类参数处理
        $Category = $this->get("xiao_zhu.category_model");
        $params = $Category->getCategoryFromString($product["searchInfo"]);

        $data = array();
        $data["product"] = $product;
        //检测评论情况
        $data["comments"] = 0;
        $session = $this->get("session");
        $oldData = $session->get("productStars");
        if (!$oldData) {
            $oldData = array();
        }
        if (array_key_exists($id,$oldData)) {
            $data["comments"] = 1;
        }
        //为App格式化数据
            //取出参数
        $appParam = array();
        foreach ($params["attr"] as $keyAndValue) {
            if ($keyAndValue["value"]["str"] != "-") {
                $appParam[] = array(
                    "attrName"=>$keyAndValue["name"]["str"],
                    "attrValue"=>$keyAndValue["value"]["str"],
                );
            }
        }
        $params = array(
            "id"   => $id,
            "title"   => $product["title"],
            "banners"   => array(
                $product["img1"],
                $product["img2"],
                $product["img3"],
            ),
            "price"   => $product["price"],
            "marketPrice"   => $product["marketPrice"],
            "params"   => $appParam,
            "sell"   => $product["sell"],
            "likes"   => $product["collect"],
            "share"   => 129,
            "pics"   => $product["intro"],
        );
        $return = $params;

        //相似商品
        $param = array();
        $param["andWhere"]["cid"] = $product["cid"];
        $param["page"] = 1;
        $param["per_page"] = 5;
        $ProductSame = $ProductModel->listProduct($param);
        //格式化数据
        //todo 打包此方法
        $ProductSames = array();
        foreach ($ProductSame as $d) {
            $ProductSames[] = array(
                "id"        => $d["id"],
                "img"       => $d["cover"],
                "title"     => $d["name"],
                "classes"     => $d["cid"],
                "price"     => $d["price"],
                "oprice"    => $d["marketPrice"],
                "praise"    => $d["collect"],
                "isNew"    => $d["isNew"],
                "isHot"    => $d["isHot"],
                "isSale"    => ceil($d["price"]/$d["marketPrice"]*10),
                "isFamous"    => $d["isFamous"],
            );
        }
        $return["productSame"] = $ProductSames;



        return $this->json_out(1,"",$return);
    }
}
